<template>
    <div style="width:100%;height:100%;overflow:hidden;text-align:center;">
        <weui-input :list="input_0_list" title="" :show_more="false" :tips="null" agreement_name=""
                    :more_label="input_0_more_label" @on-more="on_input_0_more"
                    @on-label="on_input_0_label"
                    @on-button="on_input_0_button">
            <weui-button slot="button" type="primary" :is_disable="!input_0_list[0].value || !input_0_list[1].value"
                        @on-click="on_login_button_click">登录
            </weui-button>
        </weui-input>
        <a style="font-size:14px" href="javascript:void(0)" @click="on_register_click">注册</a>
    </div>
</template>

<script>
    import WeuiInput from '../vue2componentSet/src/components/WeuiInput'
    import WeuiButton from '../vue2componentSet/src/components/WeuiButton'
    import global from '../lib/global'
    import axios from 'axios'

    export default {
        components: {
            WeuiInput, WeuiButton
        },
        data() {
            return {
                input_0_list: [{
                    label: '手机号码',
                    value: require('../lib/cookie').get('phone'),
                    type: 'tel',
                    style: 'color:#000;text-align:left'
                }, {
                    label: ['密码', '验证码'],
                    selected_label: '验证码',//'密码',
                    value: '',
                    type: 'tel',//'password',
                    button: '获取',//'',
                }],
                input_0_more_label: '设置密码',//'',
            }
        },
        mounted() {
            if (location.hash == '#/login')
                this.$store.commit('set_title', '登录旅悠')
        },
        methods: {
            on_input_0_label(index) {
                if (index == 1) {
                    if (this.input_0_list[index].selected_label == '密码') {
                        this.input_0_list[index].type = 'password'
                        this.input_0_list[index].value = ''
                        this.input_0_list[index].button = ''
                        this.input_0_more_label = ''
                        this.input_0_list.splice(2, 1)
                    } else {
                        this.input_0_list[index].type = 'tel'
                        this.input_0_list[index].value = ''
                        this.input_0_list[index].button = '获取'
                        this.input_0_more_label = '设置密码'
                    }
                }
            },
            on_input_0_button(index) {
                if (index == 1) {
                    if (this.$store.state.code_countdown != 0)
                        return
                    if (this.input_0_list[0].value.length != 11)
                        return
                    axios.post(global.server + 'getCode.do', {
                        phone: this.input_0_list[0].value
                    }).then(result => {
                        var obj = result.data
                        if (obj.result == 1) {
                            this.$store.commit('code_countdown')
                        }
                    }, result => {
                        console.log(result)
                    })
                }
            },
            on_login_button_click() {
                if (!this.input_0_list[0].value || !this.input_0_list[1].value)
                    return
                var data = {
                    phone: this.input_0_list[0].value
                }
                if (this.input_0_list[1].selected_label == '密码') {
                    data.password = require('md5')(this.input_0_list[1].value)
                } else {
                    data.code = this.input_0_list[1].value
                    if (this.input_0_more_label == '不设置密码') {
                        data.password = require('md5')(this.input_0_list[2].value)
                    }
                }
                if (global.is_debug) {
                    this.$store.commit('customer_login', {
                        phone: this.input_0_list[0].value,
                    })
                    this.$store.state.login_dialog.resolve && this.$store.state.login_dialog.resolve()
                    this.$store.commit('login_dialog', {
                        is_show: false
                    })
                    return
                }
                axios.post(global.server + 'customerLogin.do', data).then(result => {
                    var obj = result.data
                    if (obj.result == 1) {
                        this.$store.commit('customer_login', {
                            phone: this.input_0_list[0].value,
                            alipay: obj.alipay,
                            wechatQrcode: obj.wechatQrcode,
                            isPromoter: obj.isPromoter,
                            returnWay: obj.returnWay,
                        })
                        obj.isAdmin && this.$store.commit('admin_login', {
                            phone: this.input_0_list[0].value
                        })
                        if (this.$store.state.login_dialog.resolve)
                            this.$store.state.login_dialog.resolve()
                        else if (location.hash == '#/login')
                            this.$router.push('/')
                        this.$store.commit('login_dialog', {
                            is_show: false
                        })
                        require('../lib/cookie').set('phone', this.input_0_list[0].value, 7)
                    } else {
                        this.$store.commit('toast', {
                            type: 'warn',
                            content: obj.msg
                        })
                    }
                })
            },
            on_input_0_more() {
                if (this.input_0_list.length == 2) {
                    this.input_0_list.push({
                        label: '密码',
                        value: '',
                        type: 'password',
                    })
                    this.input_0_more_label = '不设置密码'
                } else {
                    this.input_0_list.splice(2, 1)
                    this.input_0_more_label = '设置密码'
                }
            },
            on_register_click() {
                this.$router.push('/register')
                this.$store.commit('login_dialog', {
                    is_show: false
                })
            }
        },
        computed: {
            code_countdown() {
                return this.$store.state.code_countdown
            }
        },
        watch: {
            code_countdown(value) {
                if (value == 0)
                    this.$set(this.input_0_list[1], 'button', '获取')
                else
                    this.$set(this.input_0_list[1], 'button', value)
            },
        },
    }
</script>
